define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kuerp/customers_user/index' + location.search,
                    add_url: 'kuerp/customers_user/add',
                    edit_url: 'kuerp/customers_user/edit',
                    del_url: 'kuerp/customers_user/del',
                    multi_url: 'kuerp/customers_user/multi',
                    import_url: 'kuerp/customers_user/import',
                    table: 'kuerp_customers_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                showExport:Config.hasExportPermission ? true : false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false, sortable: true},
                        // {field: 'admin_id', title: __('Admin_id')},
                        // {field: 'customers_code', title: __('Customers_code'), operate: 'LIKE'},
                        {field: 'customers.customer_name', title: __('Customers.customer_name'), operate: 'LIKE', align:'left',formatter: function(value, row, index){
                            return "<a href='javascript:void(0);' style='color: #4397fd' class='btn-dialog' " +
                                    "data-url='kuerp/customers/detail/ids/"+row.sid+"' data-title='客户详情' data-area=[\"80%\",\"80%\"] >" + row.customers.customer_name + "</a>";
                        }},
                        {field: 'first_connect_name', title: __('First_connect_name'), operate: 'LIKE'},
                        {field: 'first_connect_mobile', title: __('First_connect_mobile'), operate: 'LIKE'},
                        {field: 'first_connect_phone', title: __('First_connect_phone'), operate: false, visible:false},
                        {field: 'qq_wx_email', title: __('Qq_wx_email'), operate: false, visible:false},
                        {field: 'job', title: __('Job'), operate: 'LIKE'},
                        {field: 'connect_address', title: __('Connect_address'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'is_first', title: __('Is_first'), searchList: {"0":__('Is_first 0'),"1":__('Is_first 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime, sortable: true},
                        
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                searchFormVisible:true
            });
            $(document).on("click",'#btn-import-file',function() {
                Fast.api.open("kuerp/customers_user/import", "导入");
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'kuerp/customers_user/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'kuerp/customers_user/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'kuerp/customers_user/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            let q = {}
			location.search.replace(/([^?&=]+)=([^&]+)/g,function(_,k,v){q[k]=v});
            if (q.customer_id) {
                $("#c-sid").val(q.customer_id)
            }
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        import: function () {
            $(document).on("click", ".step-btn_btn", function(){
                var rel = $(this).data('rel');
                $('.ui_buttons').hide();
                $('.step_'+rel).show();
                $('.step-item').hide();
                $('#import-'+rel).show();

                $('.steps-ul').find('li').removeClass('step1-cur');
                $('.steps-ul').find('li').removeClass('step2-cur');
                $('.steps-ul').find('li').removeClass('step3-cur');
                if(!$('.step1-base').hasClass('step1-nol')){
                    $('.step1-base').addClass('step1-nol');
                }
                if(!$('.step2-base').hasClass('step2-nol')){
                    $('.step2-base').addClass('step2-nol');
                }
                if(!$('.step3-base').hasClass('step3-nol')){
                    $('.step3-base').addClass('step3-nol');
                }
                $('.'+rel+'-base').addClass(rel+'-cur');
            });
            $(document).on("click", ".btn-close", function(){
                parent.layer.closeAll()
            });
            $(document).on("click", ".finish_btn", function(){
                parent.layer.closeAll();
                parent.location.reload();
            });
            $(document).on("click", ".chk", function(){
                if(!$(this).hasClass('checked')){
                    $(this).addClass('checked');
                }else{
                    $(this).removeClass('checked');
                }
                if($('.quantity').hasClass('checked')){
                    $('#quantityStorage').show();
                }else{
                    $('#quantityStorage').hide();
                }
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $("#c-file").change(function () {
                    var marketyulanfile = $(this).val();
                    var editstatus = $("input[name='editstatus']:checked").val();
                    if (marketyulanfile) {
                        $.ajax({
                            url: "kuerp/customers_user/import",
                            data: {'editstatus':editstatus,file: marketyulanfile},
                            type: "post",
                            success: function (res) {
                                if(res.code==1){
                                    $('#addMsg').html(res.msg.add+'条');
                                    $('#updateMsg').html(res.msg.update+'条');
                                    $('#noexists').html(res.msg.noexists);
                                    Toastr.success('导入成功');
                                }else{
                                    Toastr.error(res.msg);
                                }
                            }
                        });
                    }

                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
